/*
*    Phantom v2.0 GUI Automation Software
*    Copyright (C) 2009  John C. Van Arsdall (john@phantomtest.com)
*
*    This program is free software; you can redistribute it and/or modify
*    it under the terms of the GNU General Public License as published by
*    the Free Software Foundation; either version 2 of the License, or
*    (at your option) any later version.
*
*    This program is distributed in the hope that it will be useful,
*    but WITHOUT ANY WARRANTY; without even the implied warranty of
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*    GNU General Public License for more details.
*
*    You should have received a copy of the GNU General Public License along
*    with this program; if not, write to the Free Software Foundation, Inc.,
*    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*/
#include "StdAfx.h"
#include "phantomtreeview.h"
#include "afxcview.h"

CPhantomTreeView::CPhantomTreeView(void) : CPhantomWindow(String("SysTreeView32"))
{
}

CPhantomTreeView::~CPhantomTreeView(void)
{
}

int CPhantomTreeView::FunctionProcessor(SSI_FunctionAPI& theAPI)
{
	CTreeView* View = (CTreeView*)aWnd;
	CTreeCtrl* Ctrl;
	Ctrl = &(View->GetTreeCtrl());
	
	if(theAPI.m_FunctionName == "GetCount"){
		CDataInt i;
		i.Data = (SSI_INT64)Ctrl->GetCount();
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&i);
	}else if(theAPI.m_FunctionName == "GetSelected"){
		HTREEITEM hSel = TreeView_GetSelection(aWnd->m_hWnd);;
		CDataString s;
		CStatus RetStat = GetItemText(hSel, s.Data);
		if(!RetStat){
			theAPI.m_ReturnStatus = RetStat;
		}else{
			theAPI.m_ReturnStatus = theAPI.SetReturnData(&s);
		}
	}else if(theAPI.m_FunctionName == "Select"){
		SetActive();
		CStatus RetStat;
		String s = SSI_EXTRACT(CDataString, 0);
		HTREEITEM hItem;
		RetStat = ItemFromString(s, hItem);
		if(!RetStat){
			theAPI.m_ReturnStatus = RetStat;
			return 0;
		}
		if(!TreeView_SelectItem(aWnd->m_hWnd, hItem)){
			//g_Result = TREEVIEW_BAD_SELECT;
			P_MAKEERR1(ERR_P01570, s.Str());
			return 0;
		}
		theAPI.m_ReturnStatus = STAT_OK;
	}else if(theAPI.m_FunctionName == "Expand"){
		SetActive();
		CStatus RetStat;
		String s = SSI_EXTRACT(CDataString, 0);
		HTREEITEM hItem;
		RetStat = ItemFromString(s, hItem);
		if(!RetStat){
			theAPI.m_ReturnStatus = RetStat;
			return 0;
		}
		TreeView_Expand(aWnd->m_hWnd, hItem, TVE_EXPAND);
		theAPI.m_ReturnStatus = STAT_OK;
	}else if(theAPI.m_FunctionName == "Collapse"){
		SetActive();
		CStatus RetStat;
		String s = SSI_EXTRACT(CDataString, 0);
		HTREEITEM hItem;
		RetStat = ItemFromString(s, hItem);
		if(!RetStat){
			theAPI.m_ReturnStatus = RetStat;
			return 0;
		}
		TreeView_Expand(aWnd->m_hWnd, hItem, TVE_COLLAPSE);
		theAPI.m_ReturnStatus = STAT_OK;
	}else if(theAPI.m_FunctionName == "IsExpanded"){
		SetActive();
		CStatus RetStat;
		String s = SSI_EXTRACT(CDataString, 0);
		HTREEITEM hItem;
		RetStat = ItemFromString(s, hItem);
		if(!RetStat){
			theAPI.m_ReturnStatus = RetStat;
			return 0;
		}
		
		TVITEM pItem;
		CDataBool b;
		int Val = 0;
		pItem.mask = TVIF_STATE;
		pItem.stateMask = TVIS_EXPANDED;
		pItem.hItem = hItem;
		if(!TreeView_GetItem(aWnd->m_hWnd, &pItem)){
			P_MAKEERR1(ERR_P01571, s.Str());
			return 0;
		}
		if(pItem.state & TVIS_EXPANDED){
			b.Data = 1;
		}else{
			b.Data = 0;
		}
		theAPI.m_ReturnStatus = theAPI.SetReturnData(&b);
	}else{
		return CPhantomWindow::FunctionProcessor(theAPI);
	}
	return 1;
}

CPhantomWindow* CPhantomTreeView::MakeWindow(void)
{
	return new CPhantomTreeView();
}

CStatus CPhantomTreeView::GetItemText(HTREEITEM hItem, String& Ret)
{
	TCHAR szName[1024] = "";
	LPTVITEM pItem = new TVITEM;
	HTREEITEM hSel = TreeView_GetSelection(aWnd->m_hWnd);;
	
	pItem->pszText = szName;
	pItem->cchTextMax = 1024;
	pItem->mask = TVIF_TEXT;
	pItem->hItem = hItem;
	
	if(TreeView_GetItem(aWnd->m_hWnd, pItem)){
	}else{
			//g_Result = TREEVIEW_BAD_GETTEXT;
			MAKEERR0(ERR_P01572);
	}
	Ret = szName;
	delete pItem;
	return STAT_OK;
}

CStatus CPhantomTreeView::ItemFromString(String& sItem, HTREEITEM& Ret)
{
	SSI_INT64 Pos;
	String Delims = ".";
	String Item;
	String Temp;
	HTREEITEM Curr = TreeView_GetRoot(aWnd->m_hWnd);
	CStatus RetStat;
	while((Pos = sItem.Parse(Delims, Item)) >= 0){
		while(RetStat = GetItemText(Curr, Temp)){
			if(Temp == Item) break;
			Curr = TreeView_GetNextSibling(aWnd->m_hWnd, Curr);
			if(Curr == SSI_NULL){
				// Item not found
				MAKEERR1(ERR_P01573, Item.Str());
			}
		}
		Curr = TreeView_GetChild(aWnd->m_hWnd, Curr);
	}

	Item = sItem;
	while(RetStat = GetItemText(Curr, Temp)){
		Ret = Curr;
		if(Temp == Item) break;
		Curr = TreeView_GetNextSibling(aWnd->m_hWnd, Curr);
		if(Curr == SSI_NULL){
			// Item not found
			MAKEERR1(ERR_P01574, Item.Str());
		}
	}

	return STAT_OK;
}
